package de.wittekind.bundesjugendspiele.client.management.urkunden;

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import de.wittekind.bundesjugendspiele.dummy.DummyRiege;
import de.wittekind.bundesjugendspiele.dummy.DummyRiegenLoader;
import de.wittekind.bundesjugendspiele.server.interfaces.Riege;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler;

public class Riegentabellen {
	private static final String FILE = "H:/Riegenliste.pdf";
	private static final Font catFont = new Font(Font.FontFamily.TIMES_ROMAN,
			22, Font.BOLD);
	private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 13,
			Font.BOLD);

	public static void main(String[] args) {
		try {
			Riege r = new DummyRiegenLoader().getRiege(1);
			druckeRiegenlisten(r);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Add a list createList(subCatPart); Paragraph paragraph = new Paragraph();
	 * addEmptyLine(Paragraph, 5); subCatPart.add(paragraph);
	 **/
	private static void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}

	public static void druckeRiegenlisten(Riege riege) {
		try {
			// erstellt ein neues PDF-Dokument
			Document document = new Document();
			PdfWriter.getInstance(document, new FileOutputStream(FILE));
			// �ffnet das Dokument, f�llt und schlie�t es schlie�lich
			document.open();
			Schueler eins = riege.getSchueler(1);
			
			//TODO Station erkennen
			int a = 0;
			while(a <=3)
			{
			Anchor anchor = new Anchor("Riege", catFont);
			anchor.setName("Riege");
			// RiegenID ab 0?
			Chapter catPart = new Chapter(new Paragraph(anchor),
					riege.getID() + 1);

			Paragraph paragraph = new Paragraph();
			addEmptyLine(paragraph, 1);
			catPart.add(paragraph);
			
			paragraph.add("Station:");
			addEmptyLine(paragraph, 1);
			paragraph.add(new Paragraph("Riegenführer: "+ riege.getRiegenFuehrer()));
			addEmptyLine(paragraph, 1);

			float[] colsWidth = { 0.6f, 4f, 1f, 1.1f, 4f };
			PdfPTable table = new PdfPTable(colsWidth);
			table.setWidthPercentage(100);

			PdfPCell c1 = new PdfPCell(new Phrase("Nr", subFont));
			c1.setPadding(5);
			c1.setHorizontalAlignment(Element.ALIGN_LEFT);
			table.addCell(c1);

			c1 = new PdfPCell(new Phrase("Name, Vorname", subFont));
			c1.setPadding(5);
			c1.setHorizontalAlignment(Element.ALIGN_LEFT);
			table.addCell(c1);

			c1 = new PdfPCell(new Phrase("Klasse", subFont));
			c1.setPadding(5);
			c1.setHorizontalAlignment(Element.ALIGN_LEFT);
			table.addCell(c1);

			c1 = new PdfPCell(new Phrase("Startnr", subFont));
			c1.setPadding(5);
			c1.setHorizontalAlignment(Element.ALIGN_LEFT);
			table.addCell(c1);

			c1 = new PdfPCell(new Phrase("", subFont));
			c1.setPadding(5);
			c1.setHorizontalAlignment(Element.ALIGN_LEFT);
			table.addCell(c1);
			table.setHeaderRows(1);

			int i = 1;
			
			try {
				for (int id : riege.getSchuelerIDs()) {
					Schueler s = riege.getSchueler(id);
					c1 = new PdfPCell(new Phrase("" + i));
					c1.setPadding(6);
					table.addCell(c1);
					i++;
					c1 = new PdfPCell(new Phrase(s.getLastName() + ", "
							+ s.getFirstName()));
					c1.setPadding(6);
					table.addCell(c1);

					String klasse = s.getForm();
					c1 = new PdfPCell(new Phrase(klasse));
					c1.setPadding(6);
					table.addCell(c1);

					c1 = new PdfPCell(new Phrase("" + s.getID()));
					c1.setPadding(6);
					table.addCell(c1);

					table.addCell("                                     ");
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// subCatPart.add(table);
			catPart.add(table);
			document.add(catPart);
			a++;
			}
			document.close();
		} 
		
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
